<template>
  <div>
    <PageTemplate>
      <div slot="content">
        <PageEditCollapse :is-search="false" :default-collapse="true">
          <div slot="title" class="page-edit-colllapse-title">
            <div style="width: 200px;">{{$t('cm.baseinfo')}}</div>
          </div>
          <div>
            <el-form
              :model="purBasProject"
              label-position="right"
              label-width="150px"
              class="page-edit-colllapse-form"
              :rules="rules"
              ref="purBasProject">
              <el-row>
                <el-col :span="12">
                  <el-form-item :label="$t('pur.project_code')" prop="projectCode">
                    <el-input v-model="purBasProject.projectCode"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item :label="$t('pur.project_name')" prop="projectName">
                    <el-input v-model="purBasProject.projectName"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item :label="$t('cm.is_valid')" prop="isValid">
                    <el-select v-model="purBasProject.isValid" filterable placeholder="请选择">
                      <el-option
                        v-for="(item, index) in isValids"
                        :key="index"
                        :label="item.dictText"
                        :value="item.dictKey"
                      ></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item :label="$t('pur.project_description')">
                    <el-input v-model="purBasProject.description"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </div>
        </PageEditCollapse>
        <el-row class="row-empty-10"></el-row>
        <el-row>
          <el-button type="primary" @click="submitForm('purBasProject')"  :loading="submiting" class="loading-submiting">
            {{$t('cm.commit')}}
          </el-button>
          <el-button type="primary" @click="toList">
            {{$t('cm.return')}}
          </el-button>
        </el-row>
      </div>
    </PageTemplate>
  </div>
</template>

<script>
import { ALERT } from '@/utils/responseHandle'
import * as Constant from '@/utils/constant'
import PageTemplate from '@/components/common/PageTemplate'
import PageEditCollapse from '@/components/common/PageEditCollapse'
import HasBtn from '@/mixins/HasBtn'
import { mapState } from 'vuex'
import {COMMON} from '@/utils/commonUtil'
import api_pur from '@/api/api_pur'
import Submiting from '@/mixins/Submiting'
export default {
  components: {
    PageTemplate, PageEditCollapse
  },
  mixins: [Submiting],
  data () {
    return {
      purBasProject: {
        id: null,
        projectCode: null,
        projectName: null,
        isValid: null,
        description: null
      },
      rules: {
        projectCode: [
          { required: true, message: '必填项', trigger: 'blur' }
        ],
        projectName: [
          { required: true, message: '必填项', trigger: 'blur' }
        ],
        isValid: [
          { required: true, message: '必选项', trigger: 'change' }
        ]
      }
    }
  },
  created () {
    if (this.$router.history.current.params.id !== Constant.NEW_PARAMS) {
      this.purProjectLoadGet(this.$router.history.current.params.id)
    }
  },
  methods: {
    purProjectLoadGet (id) {
      api_pur.purProjectLoadGet({id}, result => {
        if (result.code === Constant.RT_SUCCESS) {
          this.purBasProject = result.data
        } else {
          // _this.$t('cm.tips')
          ALERT.error(result.msg)
        }
      })
    },
    submitForm (formName) {
      this.openSubmiting()
      this.$refs[formName].validate((valid, error) => {
        if (valid) {
          api_pur.purProjectSave([this.purBasProject], result => {
            if (result.code === Constant.RT_SUCCESS) {
              ALERT.success(result.msg)
              this.toList()
            } else {
              // _this.$t('cm.tips')
              ALERT.error(result.msg)
            }
            this.closeSubmiting()
          })
        } else {
          this.closeSubmiting()
          return false
        }
      })
    },
    toList () {
      COMMON.routerTo(Constant.ROUTE_PUR_BAS_PROJECT.index, this.$router)
    }
  },
  mounted () {
  },
  computed: {
    ...mapState({
      isValids: state => state.dict.isValids
    })
  }
}
</script>

<style lang="scss">
</style>
